var ns;
app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        ProjectName: '',
        form: {
            startMonth: '',
            activeDateDesc: '月份',
            ProjectCompanyType: 1,
            ProjectID: 0,
            ProjectIDs: [],
        },
        projectList: [],
        minDate: new Date(),
        maxDate: new Date(),
        currentDate: 0,
        meterTypeList: [
            { id: 1, name: '房间表' },
            { id: 2, name: '公表' },
            { id: 3, name: '总表' },
        ],
        list: [],
        // list:[{"MeterCategoryID":1,"Name":"水表","Count":13,"NoWrite":13},{"MeterCategoryID":2,"Name":"电表","Count":96,"NoWrite":96}],
        metertype: 1,
        btnName: '房间表',
    },
    methods: {
        formatter(type, val) {
            if (type === 'year') {
                return `${val}年`;
            }
            if (type === 'month') {
                return `${val}月`;
            }
            return val;
        },
        onConfirm(day) {
            var that = this;
            var nowdate = moment(day).format('YYYY-MM');
            that.form.startMonth = nowdate;
            that.doSearch();
        },
        get_data: function () {
            var that = this;
            that.getSearchParams();
        },
        doSelectProject: function (ID, Name) {
            var that = this;
            that.ProjectName = Name;
            that.form.ProjectID = ID;
            that.form.ProjectIDs = [];
            that.form.ProjectIDs.push(ID);
            that.showsearchparam = false;
            that.doSearch();
        },
        //筛选数据
        getSearchParams: function () {
            var that = this;
            var options = {};
            options.action = "gettaskanalysisparams";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.projectList = data.projectList;
                    if (that.projectList != undefined && that.projectList.length > 0) {
                        that.doSelectProject(that.projectList[0].ID, that.projectList[0].Name);
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        doSelectSearchParam: function (type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;

            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            if (type == 2) { //按月 用的van-popup 自带了遮罩
                that.showsearch = false;
            }
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
            that.showPopover = false;
        },
        doSearch: function () {
            var that = this;
            that.doHideSearch();
            that.getData();
        },
        getData: function () {
            var that = this;
            var options = {};
            options.action = "getchargemeterlist"; ////方法名称
            options.ProjectID = that.form.ProjectID;
            options.WriteDate = that.form.startMonth + '-01';
            options.Type = that.metertype;
            // console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                if (succeed) {
                    that.list = data.data;
                }
                else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom '
                    });
                    return
                }
            }, {
                toast: true
            });
        },
        onClickMeterType(itemtype) {
            this.metertype = itemtype.id;
            this.btnName = itemtype.name;
            this.getData();
        },
        //抄表
        do_ReadMeter: function () {
            var that = this;
            if (that.metertype == 1) {
                //房间表
                var params = {
                    type: 1,
                    fromtype: 5,
                    selecttype: 1,
                    time: that.form.startMonth,
                };
                ns.openWin('chooseproject_frm', '选择房间', params);
            } else {
                //公摊表具：公表、总表
                ns.openWin('meterwritechoose_more', '选择仪表信息', {
                    type: that.metertype,
                    time: that.form.startMonth,
                    projectId: that.form.ProjectID,
                });
            }
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.minDate = new Date(moment(new Date()).add(-20, 'y'));
    app.maxDate = new Date(moment(new Date()).add(50, 'y'));
    app.currentDate = new Date();
    var nowdate = moment(app.currentDate).format('YYYY-MM');
    app.form.startMonth = nowdate;

    app.get_data();

};
